/**
 * 用户相关的接口
 */
import request from '../request'
import store from '../store'
import login from '../login'
import {types} from '../store/module/mine'
import config from '../../config'

if(login.getToken()){
    store.dispatch(types.LOGIN)
}

export default {
    /**
     * 用户登录
     * @param userName      邮箱
     * @param password      密码
     * @returns {Promise.<TResult>|*}
     */
    login(userName, password){
        return request.get(`${config.apiRoot}v1/oauth/token?email=${encodeURIComponent(userName)}&password=${password}`)
            .then(data=>{
                login.setToken(data.access_token)
                store.dispatch(types.LOGIN)
            })
    },

    /**
     * 根据用户id获得用户
     * @param id        用户id
     * @returns {Promise.<TResult>|*}
     */
    getUserById(id){
        return request.get(`${config.apiRoot}v1/user/${id}`)
    },
}
